// +----------------------------------------------------------------------
// | Project:   boxfish-android-commons  
// +----------------------------------------------------------------------
// | CreateTime: 15/11/30  下午2:38
// +----------------------------------------------------------------------
// | Author:     xab(xab@xabad.cn)
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------
package cn.xabad.commons.sample;

import android.content.Context;
import android.support.multidex.MultiDexApplication;
import cn.xabad.commons.sample.component.AppComponent;
import cn.xabad.commons.sample.component.DaggerAppComponent;
import cn.xabad.commons.sample.modules.AppModule;
import cn.xabad.commons.sample.tools.PathU;
import com.crashlytics.android.Crashlytics;
import com.facebook.cache.disk.DiskCacheConfig;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import io.fabric.sdk.android.Fabric;

/**
 * DESC   :
 * AUTHOR : Xabad
 */
public class SampleApplication extends MultiDexApplication {

    private static SampleApplication instance;
    private static AppComponent component;
    private RefWatcher refWatcher;

    @Override
    public void onCreate() {
        super.onCreate();
        final Fabric fabric = new Fabric.Builder(this)
                .kits(new Crashlytics())
                .debuggable(true)
                .build();
        Fabric.with(fabric);
        component = DaggerAppComponent.builder()
                                      .appModule(new AppModule(this))
                                      .build();
        instance = this;
        getComponent().inject(this);
        refWatcher = LeakCanary.install(this);
        PathU.getInstance().initDirs();
        DiskCacheConfig.Builder builder = DiskCacheConfig.newBuilder();
        builder.setBaseDirectoryPath(PathU.getProjectDir());//设置图片缓存路径
        builder.setBaseDirectoryName("image");//设置图片缓存路径（子目录）
        ImagePipelineConfig config = ImagePipelineConfig.newBuilder(context())
                                                        .setMainDiskCacheConfig(builder.build())
                                                        .build();
        Fresco.initialize(context(),config);
    }

    public RefWatcher getRefWatcher() {
        return getInstance().refWatcher;
    }

    public static SampleApplication getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private static class SingletonHolder {
        private static final SampleApplication INSTANCE = instance;
    }

    public static Context context() {
        return getInstance().getApplicationContext();
    }

    public static AppComponent getComponent() {
        return component;
    }

}
